package com.manor.common.utils

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.annotation.NonNull
import com.lxj.xpopup.interfaces.OnSelectListener
import com.manor.common.CommonAppContext
import com.manor.common.dialog.OpenMapAppDialog
import com.manor.common.R
import com.manor.common.utils.WordUtil.getString

/**
 *


 * @date  2020/6/8  14:30
 * @version 1.0
 */
class CommonUtil
private constructor() {
    companion object {
        val instance: CommonUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            CommonUtil()
        }
    }


    /**
     * 展示选择地图对话框
     */
    fun showMapDialog(mContext: Context, lat: Double, lng :Double,destination :String = "目的地" ) {
        OpenMapAppDialog.load(mContext, OnSelectListener { position, _ ->
            when (position) {
                0 -> {
                    if (isAppInstalled(OpenMapAppDialog.PACKAGE_NAME_BAIDU))
                        openMapToDaoHan(OpenMapAppDialog.PACKAGE_NAME_BAIDU, lat, lng, mContext, destination)
                    else
                        ToastUtil.show("手机未安装百度地图APP")
                }
                1 -> {
                    if (isAppInstalled(OpenMapAppDialog.PACKAGE_NAME_GAODE))
                        openMapToDaoHan(OpenMapAppDialog.PACKAGE_NAME_GAODE, lat, lng, mContext, destination)
                    else
                        ToastUtil.show("手机未安装高德地图APP")
                }
                2 -> {
                    if (isAppInstalled(OpenMapAppDialog.PACKAGE_NAME_TENGXUN))
                        openMapToDaoHan(OpenMapAppDialog.PACKAGE_NAME_TENGXUN, lat, lng, mContext, destination)
                    else
                        ToastUtil.show("手机未安装腾讯地图APP")
                }
            }
        }, mutableListOf("百度地图${getInstallStr(OpenMapAppDialog.PACKAGE_NAME_BAIDU)}",
                "高德地图${getInstallStr(OpenMapAppDialog.PACKAGE_NAME_GAODE)}",
                "腾讯地图${getInstallStr(OpenMapAppDialog.PACKAGE_NAME_TENGXUN)}"))
    }

    /**
     * 调起第三方地图APP导航
     */
    private fun openMapToDaoHan(packageName: String, lat: Double, lng :Double, context: Context?,destination :String) {
        var showToastTxt: String = ""
        try {
            when (packageName) {
                OpenMapAppDialog.PACKAGE_NAME_BAIDU -> {
                    showToastTxt = "手机未安装百度地图APP"
                    val bdLatLng = GpsCoordinateUtils.calGCJ02toBD09(lat,lng)
                    val intent = Intent()
                    //导航界面
                    intent.data = Uri.parse("baidumap://map/direction?destination=latlng:${bdLatLng[0]},${bdLatLng[1]}|name:${destination}&coord_type=bd09ll&mode=driving")
                    //由于没获取到目的地地址，所以跳到目的地界面
                    //intent.setData(Uri.parse("baidumap://map/geocoder?location=${item?.la},${item?.lg}&src=andr.baidu.openAPIdemo"))
                    context?.startActivity(intent)

                }
                OpenMapAppDialog.PACKAGE_NAME_GAODE -> {
                    showToastTxt = "手机未安装高德地图APP"
                    val intent = Intent()
                    intent.setPackage("com.autonavi.minimap")
                    intent.action = Intent.ACTION_VIEW
                    intent.addCategory(Intent.CATEGORY_DEFAULT)
                    intent.data = Uri.parse(
                            "androidamap://route?sourceApplication=${getString(R.string.app_name)}&" +
                                    "dlat=" + lat + "&dlon=" + lng + "&dname=${destination}" + "&dev=0&t=0"
                    )
                    context?.startActivity(intent)
                }
                OpenMapAppDialog.PACKAGE_NAME_TENGXUN -> {
                    showToastTxt = "手机未安装腾讯地图APP"
                    val intent = Intent()
                    intent.data = Uri.parse("qqmap://map/routeplan?type=walk&to=${destination}&tocoord=${lat},${lng}&policy=1&referer=myapp")
                    context?.startActivity(intent)
                }
            }
        } catch (ex: ActivityNotFoundException) {
            ToastUtil.show(showToastTxt)
        }
    }


    private fun getInstallStr(pkgName: String):String{
        return if (isAppInstalled(pkgName)) "" else " [未安装]"
    }

    private fun isAppInstalled(@NonNull pkgName: String?): Boolean {
        val packageManager: PackageManager = CommonAppContext.sInstance.packageManager
        return try {
            packageManager.getApplicationInfo(pkgName, 0) != null
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            false
        }
    }



}